package com.lzy.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.common.util.UserUtil;
import com.lzy.product.dao.ProductInfoMapperExt;
import com.lzy.product.dtos.ProductInfoDto;
import com.lzy.product.entity.ProductInfo;
import com.lzy.product.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by WIN7 on 2017/11/28.
 */
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    ProductInfoMapperExt productInfoMapperExt;

    @Override
    public Long insert(ProductInfoDto dto) {
        ProductInfo productInfo = DozerUtils.map(dto, ProductInfo.class);
        return productInfoMapperExt.insertSelective(productInfo) > 0 ? productInfo.getId() : null;
    }

    @Override
    public Boolean update(ProductInfoDto dto) {
        return productInfoMapperExt.updateByPrimaryKeySelective(DozerUtils.map(dto, ProductInfo.class)) > 0;
    }

    @Override
    public Boolean delete(Long id) {
        return productInfoMapperExt.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public Boolean remove(Long id) {
        return productInfoMapperExt.remove(id, UserUtil.getCurrentUser().getId()) > 0;
    }

    @Override
    public ProductInfoDto findById(Long id) {
        return DozerUtils.map(productInfoMapperExt.selectByPrimaryKey(id), ProductInfoDto.class);
    }

    @Override
    public List<ProductInfoDto> query(ProductInfoDto dto) {
        return productInfoMapperExt.selectList(dto);
    }

    @Override
    public PageInfo<ProductInfoDto> selectList(Pager pager, ProductInfoDto dto) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        List<ProductInfoDto> produceInfoDtoList = productInfoMapperExt.selectList(dto);
        return new PageInfo<>(produceInfoDtoList);
    }

    @Override
    public Integer updateList(Integer shelves, String ids) {
        return productInfoMapperExt.updateList(shelves, ids, UserUtil.getCurrentUser().getId());
    }

    @Override
    public PageInfo<ProductInfoDto> selectListByIdList(Pager pager, String ids) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        List<ProductInfoDto> produceInfoDtoList = productInfoMapperExt.selectListByIdList(ids);
        return new PageInfo<>(produceInfoDtoList);
    }

    @Override
    public PageInfo<ProductInfoDto> selectListByDept(Pager pager, ProductInfoDto dto, Long deptId) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        List<ProductInfoDto> produceInfoDtoList = productInfoMapperExt.selectListByDept(dto, deptId);
        return new PageInfo<>(produceInfoDtoList);
    }

    @Override
    public List<ProductInfoDto> selectListByClassifyId(ProductInfoDto dto, Long classifyId) {
        return productInfoMapperExt.selectListByClassifyId(dto, classifyId);
    }

    @Override
    public PageInfo<ProductInfoDto> selectListByClassifyIdList(Pager pager, ProductInfoDto dto, String ids) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        List<ProductInfoDto> productInfoDtoList = productInfoMapperExt.selectListByClassifyIdList(dto, ids);
        return new PageInfo<>(productInfoDtoList);
    }
}
